﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FU.SE0208.G1.Entities;

namespace FU.SE0208.G1.Controller
{
    public class MaintenanceHandler
    {
        public bool addnew(Maintenance obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("AddMaintenance", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@ValidFrom", SqlDbType.DateTime)).Value = obj.ValidFrom1;
                command.Parameters.Add(new SqlParameter("@ValidTo", SqlDbType.DateTime)).Value = obj.ValidTo1;
                command.Parameters.Add(new SqlParameter("@Status", SqlDbType.NVarChar)).Value = obj.Status1;
                                
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return true;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool del(Maintenance obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("DeleteMaintenance", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@MainID", SqlDbType.Int)).Value = obj.MainID1;
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return true;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool edit(Maintenance obj, Maintenance newObj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("EditMaintenance", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@MainID", SqlDbType.Int)).Value = obj.MainID1;
                command.Parameters.Add(new SqlParameter("@ValidFrom", SqlDbType.DateTime)).Value = newObj.ValidFrom1;
                command.Parameters.Add(new SqlParameter("@ValidTo", SqlDbType.DateTime)).Value = newObj.ValidTo1;
                command.Parameters.Add(new SqlParameter("@Status", SqlDbType.NVarChar)).Value = newObj.Status1;

                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return true;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public DataSet getAll(string table)
        {
            DataSet ds = new DataSet("G1");
            DbHandler.getQuery(ds,"Select * from [" + table + "]",table);
            return ds;
        }
    }
    
}
